﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace EWineShop.Cache
{
    /// <summary>
    /// 缓存提供程序工厂
    /// </summary>
    public abstract class CacheProviderFactory
    {
        #region Static Members


        /// <summary>
        /// 默认缓存提供程序工厂
        /// </summary>
        private static CacheProviderFactory @default;

        /// <summary>
        /// 进程同步锁
        /// </summary>
        private static object syncRoot = new object();

        /// <summary>
        /// 默认缓存提供程序工厂
        /// </summary>
        public static CacheProviderFactory Default
        {
            get
            {
                if (@default == null)
                {
                    lock (syncRoot)
                    {
                        if (@default == null)
                        {
                            @default = new HashTableFactory();
                        }
                    }
                }

                return @default;
            }
        }

        public static CacheProviderFactory Create(string poolName)
        {
            return null;
            //return IoC.Resolve<CacheProviderFactory>(poolName);
        }

        public static CacheProviderFactory HashTable()
        {
            return new HashTableFactory();
        }

        public static CacheProviderFactory Memcached()
        {
            MemcachedServer[] server = (MemcachedServer[])ConfigurationManager.GetSection("memcache");
            return new MemcachedFactory(server);
        }

        #endregion

        #region Functions

        /// <summary>
        /// 创建缓存提供程序
        /// </summary>
        /// <returns>缓存提供程序</returns>
        public abstract ICacheProvider CreateCacheProvider();

        /// <summary>
        /// 执行清理工作
        /// </summary>
        public abstract void ClearUp();

        #endregion
    }
}
